﻿using System;
using System.Windows.Documents;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Data;

namespace CodeCounsel.SharePoint.RibbonDesigner.View.AttachedBehaviors
{
    class FocusVisualAdorner : Adorner
    {
        // Fields
        private UIElement _adornerChild;
        private ContentElement _adornedContentElement;
        private UIElementCollection _canvasChildren;
        private IContentHost _contentHostParent;
        private ReadOnlyCollection<Rect> _contentRects;
        private Style _focusVisualStyle;
        private GeneralTransform _hostToAdornedElement;

        // Methods
        public FocusVisualAdorner(UIElement adornedElement, Style focusVisualStyle)
            : base(adornedElement)
        {
            this._hostToAdornedElement = Transform.Identity;
            Control control = new Control();
            control.Style = focusVisualStyle;
            this._adornerChild = control;
            SetBinding(DataContextProperty,
                new Binding(DataContextProperty.Name)
                {
                    Mode = BindingMode.OneWay,
                    Source = adornedElement
                }); 

            base.AddVisualChild(this._adornerChild);
        }

        //public FocusVisualAdorner(ContentElement adornedElement, UIElement adornedElementParent, IContentHost contentHostParent, Style focusVisualStyle)
        //    : base(adornedElementParent)
        //{
        //    this._hostToAdornedElement = Transform.Identity;
        //    this._contentHostParent = contentHostParent;
        //    this._adornedContentElement = adornedElement;
        //    this._focusVisualStyle = focusVisualStyle;
        //    Canvas canvas = new Canvas();
        //    this._canvasChildren = canvas.Children;
        //    this._adornerChild = canvas;
        //    base.AddVisualChild(this._adornerChild);
        //    base.IsClipEnabled = true;
        //    base.IsHitTestVisible = false;
        //    base.IsEnabled = false;
        //}

        protected override Size ArrangeOverride(Size size)
        {
            Size size2 = base.ArrangeOverride(size);
            if (this._adornedContentElement != null)
            {
                if (this._contentRects == null)
                {
                    this._canvasChildren.Clear();
                }
                else
                {
                    Rect empty = Rect.Empty;
                    IEnumerator<Rect> enumerator = this._contentRects.GetEnumerator();
                    if (this._canvasChildren.Count == this._contentRects.Count)
                    {
                        for (int i = 0; i < this._canvasChildren.Count; i++)
                        {
                            enumerator.MoveNext();
                            Rect current = enumerator.Current;
                            current = this._hostToAdornedElement.TransformBounds(current);
                            Control element = (Control)this._canvasChildren[i];
                            element.Width = current.Width;
                            element.Height = current.Height;
                            Canvas.SetLeft(element, current.X);
                            Canvas.SetTop(element, current.Y);
                        }
                        this._adornerChild.InvalidateArrange();
                    }
                    else
                    {
                        this._canvasChildren.Clear();
                        while (enumerator.MoveNext())
                        {
                            Rect rect = enumerator.Current;
                            rect = this._hostToAdornedElement.TransformBounds(rect);
                            Control control2 = new Control();
                            control2.Style = this._focusVisualStyle;
                            control2.Width = rect.Width;
                            control2.Height = rect.Height;
                            Canvas.SetLeft(control2, rect.X);
                            Canvas.SetTop(control2, rect.Y);
                            this._canvasChildren.Add(control2);
                        }
                    }
                }
            }
            ((UIElement)this.GetVisualChild(0)).Arrange(new Rect(new Point(), size2));
            return size2;
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index != 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            return this._adornerChild;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Size renderSize = new Size();
            if (this._adornedContentElement == null)
            {
                renderSize = base.AdornedElement.RenderSize;
                constraint = renderSize;
            }
            ((UIElement)this.GetVisualChild(0)).Measure(constraint);
            return renderSize;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }
    }
}
